/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.longview.io.modules.analysis.web;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.longview.io.common.config.Global;
import com.longview.io.common.persistence.Page;
import com.longview.io.common.web.BaseController;
import com.longview.io.common.utils.StringUtils;
import com.longview.io.modules.analysis.entity.GrassAnalysis;
import com.longview.io.modules.analysis.service.GrassAnalysisService;
import com.longview.io.modules.analysisresult.entity.GrassAnalysisResult;
import com.longview.io.modules.analysisresult.service.GrassAnalysisResultService;
import com.longview.io.modules.grassimgs.entity.GrassImgs;
import com.longview.io.modules.grassimgs.service.GrassImgsService;

/**
 * 草地分析Controller
 * @author zf
 * @version 2018-04-24
 */
@Controller
@RequestMapping(value = "${adminPath}/analysis/grassAnalysis")
public class GrassAnalysisController extends BaseController {

	@Autowired
	private GrassAnalysisService grassAnalysisService;
	
	@Autowired
	private GrassAnalysisResultService grassAnalysisResultService;
	
	@Autowired
	private GrassImgsService grassimgsservice;
	@ModelAttribute
	public GrassAnalysis get(@RequestParam(required=false) String id) {
		GrassAnalysis entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = grassAnalysisService.get(id);
		}
		if (entity == null){
			entity = new GrassAnalysis();
		}
		return entity;
	}
	
	@RequiresPermissions("analysis:grassAnalysis:view")
	@RequestMapping(value = {"list", ""})
	public String list(GrassAnalysis grassAnalysis, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<GrassAnalysis> page = grassAnalysisService.findPage(new Page<GrassAnalysis>(request, response), grassAnalysis); 
		model.addAttribute("page", page);
		return "modules/analysis/grassAnalysisList";
	}

	@RequiresPermissions("analysis:grassAnalysis:view")
	@RequestMapping(value = "form")
	public String form(GrassAnalysis grassAnalysis, Model model) {
		model.addAttribute("grassAnalysis", grassAnalysis);

		List<GrassImgs> list =new ArrayList<GrassImgs>();
		GrassImgs grassImgs=new GrassImgs();
		if(StringUtils.isNotBlank(grassAnalysis.getId()))
		{
			grassImgs.setAnalysisId(grassAnalysis.getId());
			list= grassimgsservice.findList(grassImgs);
		}
		model.addAttribute("list", list);
		return "modules/analysis/grassAnalysisForm";
	}

	@RequiresPermissions("analysis:grassAnalysis:edit")
	@RequestMapping(value = "save")
	public String save(GrassAnalysis grassAnalysis, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, grassAnalysis)){
			return form(grassAnalysis, model);
		}
		grassAnalysisService.save(grassAnalysis);
		addMessage(redirectAttributes, "保存草地分析成功");
		return "redirect:"+Global.getAdminPath()+"/analysis/grassAnalysis/?repage";
	}
	
	@RequiresPermissions("analysis:grassAnalysis:edit")
	@RequestMapping(value = "delete")
	public String delete(GrassAnalysis grassAnalysis, RedirectAttributes redirectAttributes) {
		grassAnalysisService.delete(grassAnalysis);
		addMessage(redirectAttributes, "删除草地分析成功");
		return "redirect:"+Global.getAdminPath()+"/analysis/grassAnalysis/?repage";
	}
	
	
	@RequiresPermissions("analysis:grassAnalysis:edit")
	@RequestMapping(value = "Examine")
	public String Examine(GrassAnalysis grassAnalysis, Model model, RedirectAttributes redirectAttributes) {
		model.addAttribute("grassAnalysis", grassAnalysis);
		List<GrassImgs> list =new ArrayList<GrassImgs>();
		GrassImgs grassImgs=new GrassImgs();
		if(StringUtils.isNotBlank(grassAnalysis.getId()))
		{
			grassImgs.setAnalysisId(grassAnalysis.getId());
			list= grassimgsservice.findList(grassImgs);
		}
		model.addAttribute("list", list);
		return "modules/analysis/grassAnalysisExamine";
		
	} 
	
	@RequiresPermissions("analysis:grassAnalysis:edit")
	@RequestMapping(value = "Examinesave")
	public String Examinesave(GrassAnalysis grassAnalysis, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, grassAnalysis)){
			return form(grassAnalysis, model);
		}
		grassAnalysisService.save(grassAnalysis);
		GrassAnalysisResult grassAnalysisResult=new GrassAnalysisResult();
		try {
			BeanUtils.copyProperties( grassAnalysisResult,grassAnalysis);
			grassAnalysisResult.setId("");
			grassAnalysisResult.setRemark(grassAnalysis.getId());
			grassAnalysisResultService.save(grassAnalysisResult);
		} catch (IllegalAccessException | InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		
		
		
		addMessage(redirectAttributes, "保存草地分析成功");
		return "redirect:"+Global.getAdminPath()+"/analysis/grassAnalysis/?repage";
	}
	

}